package com.xjgc.msibase.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjgc.exo.RestServerStandardManager;
import com.xjgc.msi.exo.wsclient.WsClientStandardRequest;
import com.xjgc.msi.exo.wsclient.WsClientStandardResponse;
import com.xjgc.msibase.constant.Constant;
import com.xjgc.msibase.entity.MsiExoServer;
import com.xjgc.msibase.entity.MsiExoServerRecord;
import com.xjgc.msibase.entity.MsiExoTaskRecord;
import com.xjgc.msibase.mapper.MsiExoServerMapper;
import com.xjgc.msibase.service.IMsiExoServerRecordService;
import com.xjgc.msibase.service.IMsiExoServerService;
import com.xjgc.msibase.vo.MsiExoSystemServerVO;
import com.xjgc.exo.WsClientStandardManager;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.entity.JeecgEntity;
import org.springframework.beans.BeanUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 外围服务接口
 *
 * @Description: 外围服务接口
 * @Author: 小逗号
 * @Date: 2023-10-30
 * @Version: V1.0
 */
@Slf4j
@Service
public class MsiExoServerServiceImpl extends ServiceImpl<MsiExoServerMapper, MsiExoServer>
        implements IMsiExoServerService {
    @Resource
    private IMsiExoServerRecordService msiExoServerRecordService;

    @Override
    public IPage<MsiExoSystemServerVO> querySystemServerPage(IPage<MsiExoSystemServerVO> page, MsiExoSystemServerVO queryAo) {
        return baseMapper.selectSystemServerVO(page, queryAo);
    }

    @Override
    public Result<?> addServer(MsiExoSystemServerVO addAo) {
        String id = addAo.getId();
        String exoServerType = addAo.getExoServerType();
        String exoSystemId = addAo.getExoSystemId();
        String exoServerCode = addAo.getExoServerCode();
        List<MsiExoServer> list = this.list(
                new QueryWrapper<MsiExoServer>().lambda()
                        .eq(MsiExoServer::getExoSystemId, exoSystemId)
                        .eq(MsiExoServer::getExoServerCode, exoServerCode)
        );
        if (CollectionUtils.isNotEmpty(list)) {
            return Result.error(addAo.getExoSystemCode() + "系统," + addAo.getExoServerCode() + "接口已存在,请勿重复添加!");
        }
        MsiExoServer msiExoServer = new MsiExoServer();
        BeanUtils.copyProperties(addAo, msiExoServer);
        this.save(msiExoServer);
        return Result.OK();

    }

    //    @Async(Constant.ASYNC_FF)
    @Async
    @Override
    public void request(MsiExoServer exoServer, MsiExoTaskRecord taskRecord, String data) {
        String exoServerCode = exoServer.getExoServerCode();
        String exoServerType = exoServer.getExoServerType();
        /*taskRecord.getTaskName();*/
        log.info("请求外部服务{}接口类型{}", exoServerCode, exoServerType);

        MsiExoServerRecord serverRecord = new MsiExoServerRecord(exoServer, taskRecord);
        serverRecord.setDataText(data);
        msiExoServerRecordService.save(serverRecord);
        if (Constant.SERVER_TYPE_WS.equals(exoServerType)) {
            log.info("发送WS请求");
            WsClientStandardResponse response = this.requestWs(exoServer, serverRecord);
        } else if (Constant.SERVER_TYPE_REST.equals(exoServerType)) {
            log.info("发送REST请求");
            Result<?> result = this.requestREST(exoServer, serverRecord);


        } else {
            log.warn("{}未知接口类型!", exoServerType);
        }
    }

    /**
     * 发送WS请求
     *
     * @param exoServer
     * @param serverRecord
     * @return
     */
    private WsClientStandardResponse requestWs(MsiExoServer exoServer, MsiExoServerRecord serverRecord) {
        WsClientStandardManager wsClientManager = new WsClientStandardManager();
        String serialNo = serverRecord.getId();
        String taskName = serverRecord.getTaskName();
        String xml = serverRecord.getDataText();
        WsClientStandardRequest request = new WsClientStandardRequest(serialNo, taskName, xml);
        WsClientStandardResponse response = wsClientManager.callWs(exoServer, request);
        String serialNoBack = response.getSerialNo();
        String state = response.getState();
        String message = response.getMessage();
        msiExoServerRecordService.update(
                new UpdateWrapper<MsiExoServerRecord>().lambda()
                        .eq(JeecgEntity::getId, serialNoBack)
                        .set(MsiExoServerRecord::getStatus, state)
                        .set(MsiExoServerRecord::getMessage, message)
        );
        return response;
    }

    /**
     * @param exoServer
     * @param serverRecord
     * @return
     */
    private Result<?> requestREST(MsiExoServer exoServer, MsiExoServerRecord serverRecord) {
        String serialNo = serverRecord.getId();
        String taskName = serverRecord.getTaskName();
        String jsonStr = serverRecord.getDataText();
        WsClientStandardRequest request = new WsClientStandardRequest(serialNo, taskName, jsonStr);


        RestServerStandardManager restServerStandardManager = new RestServerStandardManager();



        return null;
    }
}
